package org.Yiran.timetale_re.worldgen;

import net.minecraft.core.HolderGetter;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.worldgen.BootstapContext;
import net.minecraft.data.worldgen.features.FeatureUtils;
import net.minecraft.data.worldgen.features.OreFeatures;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
import net.minecraft.world.level.levelgen.feature.Feature;
import net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration;
import net.minecraft.world.level.levelgen.structure.templatesystem.TagMatchTest;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import org.Yiran.timetale_re.Timetale_re;
import org.Yiran.timetale_re.item.material.ModMaterials;
import org.Yiran.timetale_re.worldgen.feature.config.CrystalClusterConfig;

import java.util.List;

/**
 * 配置特征注册类
 * <p>
 * 这个类用于注册和管理所有配置特征（ConfiguredFeature）。
 * 配置特征定义了生成什么（例如矿石、树木等）以及如何生成（参数）。
 * </p>
 */
public class ModConfiguredFeatures {
    
    /**
     * 配置特征注册器
     */
    public static final DeferredRegister<ConfiguredFeature<?, ?>> CONFIGURED_FEATURES = 
            DeferredRegister.create(Registries.CONFIGURED_FEATURE, Timetale_re.MODID);

    // 银矿石配置特征
    public static final ResourceKey<ConfiguredFeature<?, ?>> OVERWORLD_SILVER_ORE_KEY = createKey("silver_ore");
    public static final ResourceKey<ConfiguredFeature<?, ?>> OVERWORLD_DEEPSLATE_SILVER_ORE_KEY = createKey("deepslate_silver_ore");
    
    // 铝矿石配置特征
    public static final ResourceKey<ConfiguredFeature<?, ?>> OVERWORLD_ALUMINUM_ORE_KEY = createKey("aluminum_ore");
    public static final ResourceKey<ConfiguredFeature<?, ?>> OVERWORLD_DEEPSLATE_ALUMINUM_ORE_KEY = createKey("deepslate_aluminum_ore");
    
    // 钛矿石配置特征
    public static final ResourceKey<ConfiguredFeature<?, ?>> OVERWORLD_TITANIUM_ORE_KEY = createKey("titanium_ore");
    public static final ResourceKey<ConfiguredFeature<?, ?>> OVERWORLD_DEEPSLATE_TITANIUM_ORE_KEY = createKey("deepslate_titanium_ore");
    
    // 水晶簇配置特征
    public static final ResourceKey<ConfiguredFeature<?, ?>> CRYSTAL_CLUSTER_KEY = createKey("crystal_cluster");
    
    // 能晶簇配置特征
    public static final ResourceKey<ConfiguredFeature<?, ?>> OVERWORLD_ENERGY_CRYSTAL_CLUSTER_KEY = createKey("overworld_energy_crystal_cluster");
    public static final ResourceKey<ConfiguredFeature<?, ?>> NETHER_ENERGY_CRYSTAL_CLUSTER_KEY = createKey("nether_energy_crystal_cluster");

    /**
     * 注册配置特征
     * <p>
     * 此方法应在模组主类的构造函数中调用，以正确注册所有配置特征
     * </p>
     *
     * @param modEventBus 模组事件总线
     */
    public static void register(IEventBus modEventBus) {
        CONFIGURED_FEATURES.register(modEventBus);
    }
    
    /**
     * 创建配置特征注册键
     * <p>
     * 创建一个用于注册配置特征的资源键
     * </p>
     *
     * @param name 特征名称
     * @return 配置特征的资源键
     */
    public static ResourceKey<ConfiguredFeature<?, ?>> createKey(String name) {
        return ResourceKey.create(Registries.CONFIGURED_FEATURE, ResourceLocation.fromNamespaceAndPath(Timetale_re.MODID, name));
    }
    
    /**
     * 引导配置特征
     * <p>
     * 用于数据生成的引导方法
     * </p>
     *
     * @param context 引导上下文
     */
    public static void bootstrap(BootstapContext<ConfiguredFeature<?, ?>> context) {
        HolderGetter<ConfiguredFeature<?, ?>> configuredFeatures = context.lookup(Registries.CONFIGURED_FEATURE);
        
        // 注册银矿石配置特征
        if (ModMaterials.SILVER_MATERIAL.getOre() != null) {
            List<OreConfiguration.TargetBlockState> silverOres = List.of(
                OreConfiguration.target(new TagMatchTest(BlockTags.STONE_ORE_REPLACEABLES), 
                    ModMaterials.SILVER_MATERIAL.getOre().get().defaultBlockState())
            );
            FeatureUtils.register(context, OVERWORLD_SILVER_ORE_KEY, Feature.ORE, 
                new OreConfiguration(silverOres, ModMaterials.SILVER_MATERIAL.getVeinSizeMax()));
        }
        
        if (ModMaterials.SILVER_MATERIAL.getDeepslateOre() != null) {
            List<OreConfiguration.TargetBlockState> deepslateSilverOres = List.of(
                OreConfiguration.target(new TagMatchTest(BlockTags.DEEPSLATE_ORE_REPLACEABLES), 
                    ModMaterials.SILVER_MATERIAL.getDeepslateOre().get().defaultBlockState())
            );
            FeatureUtils.register(context, OVERWORLD_DEEPSLATE_SILVER_ORE_KEY, Feature.ORE, 
                new OreConfiguration(deepslateSilverOres, ModMaterials.SILVER_MATERIAL.getDeepslateVeinSizeMax()));
        }
        
        // 注册铝矿石配置特征
        if (ModMaterials.ALUMINUM_MATERIAL.getOre() != null) {
            List<OreConfiguration.TargetBlockState> aluminumOres = List.of(
                OreConfiguration.target(new TagMatchTest(BlockTags.STONE_ORE_REPLACEABLES), 
                    ModMaterials.ALUMINUM_MATERIAL.getOre().get().defaultBlockState())
            );
            FeatureUtils.register(context, OVERWORLD_ALUMINUM_ORE_KEY, Feature.ORE, 
                new OreConfiguration(aluminumOres, ModMaterials.ALUMINUM_MATERIAL.getVeinSizeMax()));
        }
        
        if (ModMaterials.ALUMINUM_MATERIAL.getDeepslateOre() != null) {
            List<OreConfiguration.TargetBlockState> deepslateAluminumOres = List.of(
                OreConfiguration.target(new TagMatchTest(BlockTags.DEEPSLATE_ORE_REPLACEABLES), 
                    ModMaterials.ALUMINUM_MATERIAL.getDeepslateOre().get().defaultBlockState())
            );
            FeatureUtils.register(context, OVERWORLD_DEEPSLATE_ALUMINUM_ORE_KEY, Feature.ORE, 
                new OreConfiguration(deepslateAluminumOres, ModMaterials.ALUMINUM_MATERIAL.getDeepslateVeinSizeMax()));
        }
        
        // 注册钛矿石配置特征
        if (ModMaterials.TITANIUM_MATERIAL.getOre() != null) {
            List<OreConfiguration.TargetBlockState> titaniumOres = List.of(
                OreConfiguration.target(new TagMatchTest(BlockTags.STONE_ORE_REPLACEABLES), 
                    ModMaterials.TITANIUM_MATERIAL.getOre().get().defaultBlockState())
            );
            FeatureUtils.register(context, OVERWORLD_TITANIUM_ORE_KEY, Feature.ORE, 
                new OreConfiguration(titaniumOres, ModMaterials.TITANIUM_MATERIAL.getVeinSizeMax()));
        }
        
        if (ModMaterials.TITANIUM_MATERIAL.getDeepslateOre() != null) {
            List<OreConfiguration.TargetBlockState> deepslateTitaniumOres = List.of(
                OreConfiguration.target(new TagMatchTest(BlockTags.DEEPSLATE_ORE_REPLACEABLES), 
                    ModMaterials.TITANIUM_MATERIAL.getDeepslateOre().get().defaultBlockState())
            );
            FeatureUtils.register(context, OVERWORLD_DEEPSLATE_TITANIUM_ORE_KEY, Feature.ORE, 
                new OreConfiguration(deepslateTitaniumOres, ModMaterials.TITANIUM_MATERIAL.getDeepslateVeinSizeMax()));
        }
        
        // 注册水晶簇配置特征 - 调整参数使生成更加自然
        FeatureUtils.register(context, CRYSTAL_CLUSTER_KEY, ModWorldGen.CRYSTAL_CLUSTER_FEATURE.get(), 
                new CrystalClusterConfig(
                        3,   // minClusterSize - 减少最小簇大小
                        10,  // maxClusterSize - 增加最大簇大小
                        1,   // minCrystalLevel
                        3,   // maxCrystalLevel
                        0.05, // surfaceGenerationChance - 降低地表生成概率
                        0.2, // caveGenerationChance - 调整洞穴生成概率
                        0.5  // largeCaveGenerationChance - 调整大型洞穴生成概率
                ));
        
        // 注册能晶簇配置特征 - 主世界
        FeatureUtils.register(context, OVERWORLD_ENERGY_CRYSTAL_CLUSTER_KEY, ModWorldGen.ENERGY_CRYSTAL_CLUSTER_FEATURE.get(), 
                new CrystalClusterConfig(
                        1,   // minClusterSize
                        4,   // maxClusterSize
                        1,   // minCrystalLevel
                        4,   // maxCrystalLevel
                        1, // surfaceGenerationChance - 提高地表生成概率
                        1, // caveGenerationChance - 提高洞穴生成概率
                        1  // largeCaveGenerationChance - 提高大型洞穴生成概率
                ));
                
        // 注册能晶簇配置特征 - 下界（调整生成概率以适应下界环境）
        FeatureUtils.register(context, NETHER_ENERGY_CRYSTAL_CLUSTER_KEY, ModWorldGen.ENERGY_CRYSTAL_CLUSTER_FEATURE.get(), 
                new CrystalClusterConfig(
                        1,   // minClusterSize
                        4,   // maxClusterSize
                        1,   // minCrystalLevel
                        4,   // maxCrystalLevel
                        1, // surfaceGenerationChance - 提高生成概率（在下界中这个参数用作通用生成概率）
                        1, // caveGenerationChance - 提高洞穴生成概率
                        1  // largeCaveGenerationChance - 提高大型洞穴生成概率
                ));
    }
}